<?php

namespace App\Http\Controllers;

use App\Models\Module;
use App\Models\Org;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

class ModuleController extends Controller
{
    public function index(Request $request)
    {
//        Log::info("hellO");
        $query = Module::query()->get()->toArray();
        return $this->returnJSON($query);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'label' => 'required|max:255',
            'icon' => 'required|max:255',
            'optional' => 'required|max:32',
            'key' => 'required|unique:module|max:255',
            'settings' => 'required']);
        $data = $request->only([
            'label',
            'icon',
            'optional',
            'key',
            'settings'
        ]);
        $data['label'] = isset($data['label'])?$data['label']:'';
        $data['key'] = isset($data['key'])?$data['key']:'';
        $data['settings'] = isset($data['settings'])?$data['settings']:'';
        $node = new Module();
        $node->fill($data);
        if($node->save()){
            return $this->returnJSON(null, 0, '新增成功');
        }
        return $this->returnJSON(null, 0, '新增失败');

    }

    public function update(Request $request, $id)
    {
        $node = Module::findOrFail($id);
        $this->validate($request, [
            'label' => 'required|max:255',
            'icon' => 'required|max:255',
            'optional' => 'required|max:32',
            // 'key' => 'required|unique:module|max:255',
            'key' => [
                'required',
                'max:255',
                Rule::unique('module')->ignore($node->id)
            ],
            'settings' => 'required']);
        $data = $request->only([
            'label',
            'icon',
            'optional',
            'key',
            'settings'
        ]);
        $data['label'] = isset($data['label'])?$data['label']:'';
        $data['key'] = isset($data['key'])?$data['key']:'';
        $data['settings'] = isset($data['settings'])?$data['settings']:'';
        $node->fill($data);
        if($node->save()){
            return $this->returnJSON(null, 0, '更新成功');
        }
        return $this->returnJSON(null, 0, '更新失败');
    }

    public function setOrgModule(Request $request, $org_id){
        $modules = $request->input('modules', '');

        $node = Org::findOrFail($org_id);
        $node->fill(['module_settings' => $modules]);
        if($node->save()){
            return $this->returnJSON(null, 0, '保存机构模块成功');
        }
        return $this->returnJSON(null, 0, '保存机构模块失败');
    }

    public function options() {
        $nodes = Module::query()->get()->toArray();
        return $this->returnJSON([
            'options' => array_map(function($item) {
                return [
                    'label' => $item['label'],
                    'key' => $item['key'],
                    'icon' => $item['icon'],
                    'settings' => $item['settings']
                ];
            }, $nodes)
        ]);
    }

}
